//
//  EditRouteViewController.m
//  Follow Me
//
//  Created by apple on 2017/4/21.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "EditRouteViewController.h"
#import "takePhoto.h"
#import "Users+CoreDataClass.h"
#import "Routes+CoreDataClass.h"
#import "Comments+CoreDataClass.h"
#import "MainModel.h"

@interface EditRouteViewController ()<UITextFieldDelegate,UITextViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,strong) NSData *coverPic;

@end

@implementation EditRouteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userLogoImage.layer.cornerRadius = 45;
    self.userLogoImage.layer.borderWidth = 0.7f;
    self.userLogoImage.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.userLogoImage.layer.masksToBounds = YES;
    // Do any additional setup after loading the view.
    NSArray *userExist = [Users findByAttribute:@"userName" withValue:[MainModel model].userMsg.userName];
    Users *ExistUserInfo = [userExist firstObject];
    UIImage *currentPNG =[UIImage imageWithData:ExistUserInfo.userPicture];
    self.userLogoImage.image = currentPNG;
    self.showUserNameLabel.text = ExistUserInfo.userName;
    
    self.EditRouteNameTextField.delegate = self;
    self.EditRouteDescriptionTextView.delegate = self;
    self.EditDsetinationNameTextField.delegate = self;
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.EditRouteNameTextField.borderStyle = UITextBorderStyleNone;
    self.EditDsetinationNameTextField.borderStyle = UITextBorderStyleNone;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.navigationController.navigationBar.frame = CGRectMake(0, 20, 375, 40);
    
    [self.navigationItem setHidesBackButton:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击空白处隐藏键盘objective-c
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.EditRouteNameTextField endEditing:YES];
    [self.EditRouteDescriptionTextView endEditing:YES];
}

//完成输入后return隐藏键盘objective-c
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(BOOL)textViewShouldReturn:(UITextView *)textView
{
    [textView resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)saveRouteNameAndRouteDescriptionAndRouteCoverImage:(id)sender {
//    NSArray *routeDistance = [Routes findByAttribute:@"distance" withValue:[MainModel model].routeMsg.distance];
//    Routes *currentRoute = [routeDistance firstObject];
    Routes *currentRoute = [MainModel model].routeMsg;
    currentRoute.routeName = self.EditRouteNameTextField.text;
    currentRoute.routeDescription = self.EditRouteDescriptionTextView.text;
    currentRoute.introducePicture = self.coverPic;
    currentRoute.desName = self.EditDsetinationNameTextField.text;
    currentRoute.user = [MainModel model].userMsg;
    NSArray *sameDestination = [Destinations findByAttribute:@"destinationName" withValue:self.EditDsetinationNameTextField.text];
    if (sameDestination.count == 0){
    Destinations *destinationInfo = [Destinations createEntity];
    destinationInfo.destinationName = self.EditDsetinationNameTextField.text;
    destinationInfo.backgroundPIC = self.coverPic;
        destinationInfo.route= [NSSet setWithObjects:currentRoute, nil];}
    else{
        Destinations *oldOne = [sameDestination lastObject];
        oldOne.route = [NSSet setWithObjects:currentRoute, nil];
    }
    
    
    [[NSManagedObjectContext defaultContext] saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
        NSLog(@"%@",currentRoute);
        
    }];
    SaveRouteViewController *SaveRVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SaveRoute"];
    [self.navigationController pushViewController:SaveRVC animated:YES];
    
}
- (IBAction)AddCoverImage:(id)sender {
    [takePhoto sharePicture:^(UIImage *HeadImage){
        
        [self.AddRouteCoverImageButton setBackgroundImage:HeadImage forState:UIControlStateNormal];
        self.coverPic = UIImageJPEGRepresentation(HeadImage,0.1);
    }];
    
    [self.AddRouteCoverImageButton setTitle:@"" forState:UIControlStateNormal];
    
    
}

@end
